/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.hutech09.jsale.bsn.impl;

import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import org.hutech09.jsale.bsn.IAccessUserManager;
import org.hutech09.jsale.bsn.common.DataExistsException;
import org.hutech09.jsale.bsn.common.DataNotExistedException;
import org.hutech09.jsale.bsn.common.IGenericDao;
import org.hutech09.jsale.bsn.exception.ItemExistsException;
import org.hutech09.jsale.bsn.exception.ItemNotFoundException;
import org.hutech09.jsale.entity.AccessGroup;
import org.hutech09.jsale.entity.AccessUser;

/**
 *
 * @author Nam
 */
@Stateless
public class AccessUserManager implements IAccessUserManager {

    @EJB
    private IGenericDao dao;

    @Override
    public List<AccessUser> getAllUser() {
        return dao.getAll(AccessUser.class);
    }

    @Override
    public List<AccessGroup> getAllGroup() {
        return dao.getAll(AccessGroup.class);
    }

    @Override
    public List<AccessGroup> getGroupUserIn(String userId) throws ItemNotFoundException {
        AccessUser user = dao.find(AccessUser.class, userId);
        if (user == null) {
            throw new ItemNotFoundException("AccessUser[id=" + userId + "]");
        }
        return user.getAccessGroups();
    }

    @Override
    public AccessUser create(AccessUser user) throws ItemExistsException {
        if (user == null || user.getId() == null) {
            throw new IllegalArgumentException("user and user.id can't be null. " + user);
        }
        try {
            user = dao.create(user);
            return user;
        } catch (DataExistsException ex) {
            throw new ItemExistsException(user);
        }
    }

    @Override
    public AccessUser delete(String userId) throws ItemNotFoundException {
        if (userId == null) {
            throw new IllegalArgumentException("userId can't be null");
        }
        try {
            return dao.delete(AccessUser.class, userId);
        } catch (DataNotExistedException ex) {
            throw new ItemNotFoundException("AccessUser[id = " + userId + "]");
        }
    }

    @Override
    public void addUserToGroup(String userId, String groupId) throws ItemNotFoundException {
        if (userId == null || groupId == null) {
            throw new IllegalArgumentException("userId and role can't be null");
        }
        AccessUser user = dao.find(AccessUser.class, userId);
        if (user == null) {
            throw new ItemNotFoundException("AccessUser[id = " + userId + "]");
        }
        AccessGroup group = dao.find(AccessGroup.class, groupId);
        if (group == null) {
            throw new ItemNotFoundException("AccessGroup[id = " + groupId + "]");
        }
        user.addAccessGroup(group);
        try {
            dao.update(user);
        } catch (DataNotExistedException ex) {
            throw new ItemNotFoundException("AccessUser[id = " + userId + "]");
        }
    }

    @Override
    public void removeUserFormGroup(String userId, String groupId) throws ItemNotFoundException {
        if (userId == null || groupId == null) {
            throw new IllegalArgumentException("userId and role can't be null");
        }
        AccessUser user = dao.find(AccessUser.class, userId);
        if (user == null) {
            throw new ItemNotFoundException("AccessUser[id = " + userId + "]");
        }
        AccessGroup group = dao.find(AccessGroup.class, groupId);
        if (group == null) {
            throw new ItemNotFoundException("AccessGroup[id = " + groupId + "]");
        }
        user.removeAccessGroup(group);
        try {
            dao.update(user);
        } catch (DataNotExistedException ex) {
            throw new ItemNotFoundException("AccessUser[id = " + userId + "]");
        }
    }

    @Override
    public AccessUser update(AccessUser accessUser) throws ItemNotFoundException {
        try {
            AccessUser user = dao.update(accessUser);
            return user;
        } catch (DataNotExistedException ex) {
            throw new ItemNotFoundException(accessUser);
        }
    }

    @Override
    public AccessUser findById(String userId) {
        return  dao.find(AccessUser.class, userId);
    }
    
    
}
